// pages/mysale/get_cash/get_cash.js
const app = getApp();
var router = require("../../../router.js");
Page({
    
  /**
   * 页面的初始数据
   */
  data: {
    balance: 0.00,
    get_cash_way: 'wx',
    user_name: '',
    user_account: '',
    user_bank: '',
    money: '',
  },

  change_get_cash_way(e) {
    console.log(e.currentTarget.dataset.way);
    this.setData({
      get_cash_way: e.currentTarget.dataset.way
    })
  },

  money_change(e){
    this.setData({
      money: e.detail.value
    })
  },

  name_change(e){
    this.setData({
        user_name: e.detail.value
    })
  },

  account_change(e) {
    this.setData({
        user_account: e.detail.value
    })
  },

  bank_change(e){
    this.setData({
        user_bank: e.detail.value
    })
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  sub_get_cash() {

    var money = this.data.money;
    var way = this.data.get_cash_way;
    var name = this.data.user_name;
    var account = this.data.user_account;   
    var balance = this.data.balance; 
    
    if (balance < 100 ){
      wx.showToast({
        title: '余额不足',
        icon: 'none',
        duration: 2000
      })
      return false;
    }

    if (!money || !name || !account){
      wx.showToast({
        title: '请填写正确的提现信息',
        icon: 'none',
        duration: 2000
      })
      return false;
    }

    if (isNaN(money) || money < 100) {
      wx.showToast({
        title: '提现金额不能小于100且只能是数字',
        icon: 'none',
        duration: 2000
      })
      return false;
    }     

    var bank = this.data.user_bank;
    if (way == 'yl' && !bank) {
      wx.showToast({
        title: '请填写开户行信息',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    var that = this;
    app.request({
      url: router.user.with_add,
      method: "POST",
      data: {
        userId: wx.getStorageSync('userInfo').id,
        money: money, 
        way: way, 
        name: name, 
        account: account, 
        bank: bank
      },
      success: function (data) {        
        if (data.status == true) {
          wx.showToast({
            title: '申请提现成功',
            icon: 'success',
            duration: 2000
          })
          setTimeout(() => {          
            wx.navigateBack({
              delta: 1
            })
          }, 1000)        
        } else {
          wx.showToast({
            title: '申请提现失败',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    app.request({
      url: router.user.withdraw,
      method: "POST",
      data: {
        userId: wx.getStorageSync('userInfo').id,
      },
      success: function (data) {
        console.log(data);  
        if (data.status == true) {        
          that.setData({
            balance: data.list.balance,            
          });        
        } else {
          wx.showToast({
            title: '获取信息失败',
            icon: 'none',
            mask: true,
            duration: 2000
          });
        }
      }
    })
  }
})